<template>
  <!--设备信息-->
  <basic-dialog v-model="dialog.showDialog" :title="dialog.title" width="1500"
                :autoClose="false" @submit="submitDialog" submitBtnTxt="保存">
    <basic-crud  ref="crudRef" :option="options" :selectType="!onlyShow?'multiple':''"
                 apiGetListUrl="/mes/processTaskController/listDeviceByProcess"
                 :defaultListParams="{}" :autoLoad="true"
                 :fastSearch="{name:'型号、名称',fastColumn:'specification,name'}"
                 :apiListParams="listParams" :cell-style="cellStyle">
    </basic-crud>
  </basic-dialog>
</template>

<script setup>
import {defineExpose, ref} from "vue";
import { formatDate } from "@/utils/formatTime";
import {ElMessage} from "element-plus";
import {listLinkedDeviceByProcess} from "@/apis/mes/processTask.js";
import {apiPostBus} from "@/apis/device/deviceBorrowBill.js";

const emits = defineEmits(['refresh-parent'])

const dialog = ref({
  showDialog: false,
  title: "设备信息",
  showFormDialog:false
})
const parentRow = ref({});
const onlyShow = ref(false);
const listParams=ref({})
const linkedDevices=ref([])
// 打开新增编辑对话框
const openDialog = (row, onlyShow) => {
  dialog.value.showDialog = true;
  parentRow.value=row;
  onlyShow.value=onlyShow;
  listParams.value={
    processTaskId:parentRow.value.processTaskId,
    deviceId:row.mesId
  }
  listLinkedDeviceByProcess({
    processTaskId:parentRow.value.processTaskId,
    deviceId:row.mesId
  }).then((data) =>{
    linkedDevices.value=data;
  });
};

const submitDialog=()=> {
  if (crudRef.value.selectData.length == 0) {
    return ElMessage.error(`您没有选择任何的设备实物`);
  } else {
    var accounts = [];
    for (let i = 0; i < crudRef.value.selectData.length; i++) {
      accounts.push({
        deviceAccountId: crudRef.value.selectData[i].id,
      });
    }
    for (let i = 0; i < linkedDevices.value.length; i++) {
      accounts.push({
        deviceAccountId: linkedDevices.value[i].id,
      });
    }

    let submitData = {
      useBus: parentRow.value.bus,
      useBusId: parentRow.value.processTaskId,
      billDTO: {
        borrowReason: parentRow.value.name + "工序加工的时候使用",
        fastBorrowFlag: true,
        deviceBorrowItemDTOList: accounts,
      },
    };
    apiPostBus(submitData).then((res) => {
      ElMessage.success("选择使用的设备完成");
      emits('refresh-parent')
      dialog.value.showDialog = false;
    });
  }
}

const cellStyle=({row,column})=> {
  if (row.alertFlag === 'true') {
    if (column.columnKey === 'effectiveDate') {
      return {
        color: '#F0B100',
      };
    }
  }
}
const crudRef=ref(null);
const options= {
  addBtn: false,
  cellBtn: false,
  delBtn: false,
  editBtn: false,
  menu: false,
  searchIndex: 4,
  searchMenuPosition: 'right',
  searchSpan: 4,
  searchIcon: true,
  selectable: function (row) {
    return row.status === 'normal';
  },
  column: [{
    label: "型号/规格",
    prop: "specification",
    search: true,
    minWidth: 150
  }, {
    label: "名称",
    prop: "name",
    search: true,
    minWidth: 150
  }, {
    label: "类型",
    prop: "deviceClassifyName",
    width: 90
  }, {
    label: "实物编号",
    prop: "code",
    search: true,
    width: 150
  }, {
    label: '查看全部设备',
    searchLabelWidth: 140,
    prop: 'queryAll',
    type: 'switch',
    display: false,
    width: 120,
    search: true,
    hide: true,
    searchType: 'switch',
    value: 'false',
    dicData: [{
      label: '否',
      value: 'false'
    }, {
      label: '是',
      value: 'true'
    }]
  },
    {
      label: "有效期",
      prop: "effectiveDate",
      type: 'date',
      format: 'yyyy-MM-dd',
      width: 120,
      formatter: (row) => new Date(row.effectiveDate).getFullYear() >= 2099 ? '/' : formatDate(row.effectiveDate, "yyyy-MM-dd")
    },
    {
      label: "供应商",
      prop: "supplier",
      search: true,
      width: 120
    },
    {
      label: "参数",
      prop: "params",
      search: true,
      width: 150
    },
    {
      label: "工艺备注",
      prop: "remark",
      hide: onlyShow.value
    }
  ],
}

defineExpose({
  openDialog
})
</script>

<style scoped>

</style>